// http://47.244.30.193/swagger/ui/index#/
//封装过期控制代码
var _setItem = function(key, value, time) {
    localStorage.setItem(key, JSON.stringify({ data: value, time: time }));
};
var _getItem = function(key) {
    var data = localStorage.getItem(key);
    if (!data || String(data).toLowerCase() == 'null') {
        return '';
    }
    var dataObj = JSON.parse(data);
    if ((dataObj.time - Date.parse(new Date()) / 1000) < 0) {
        localStorage.setItem(key, '');
        return '';
    } else {
        return dataObj.data;
    }
};
console.log(_getItem('token'))

//验证手机号正则
var _checkMobile = function(sMobile) {
    let str = String(sMobile).trim();
    if (str.length == 11 && str.slice(0, 1) == "1") {
        return true;
    } else {
        _layer("请输入正确的手机号");
        return false;
    }
};

//验证身份证
var _checkIdCard = function(card) {
    // 身份证号码为15位或者18位，15位时全为数字，18位前17位为数字，最后一位是校验位，可能为数字或字符X  
    var reg = /(^\d{15}$)|(^\d{18}$)|(^\d{17}(\d|X|x)$)/;
    if (reg.test(card) === false) {
        return false;
    }
    return true;
}

//转换时间戳
var _date = function(timestamp) {
    if (!timestamp) {
        return "";
    }
    var date;
    //时间戳为10位需*1000，时间戳为13位的话不需乘1000
    if (String(timestamp).length == 10) {
        date = new Date(Number(timestamp) * 1000);
    }
    if (String(timestamp).length == 13) {
        date = new Date(Number(timestamp));
    }
    let Y = date.getFullYear() + "-";
    let M =
        (date.getMonth() + 1 < 10 ?
            "0" + (date.getMonth() + 1) :
            date.getMonth() + 1) + "-";
    let D = date.getDate() + " ";
    let h = date.getHours() + ":";
    let m = date.getMinutes() + ":";
    let s = date.getSeconds();
    return Y + M + D + h + m + s;
};

//钱分转换为元
var _money = function(val) {
    let judge = String(val);
    if (judge == "undefined" || judge == "null") {
        return 0.0;
    }
    let priceStr = String(parseFloat(parseInt(val) / 100));
    let index = priceStr.indexOf(".");
    if (index != -1) {
        let handle = priceStr.slice(index + 1);
        if (handle.length == 1) {
            priceStr = priceStr + "0";
        }
    } else {
        priceStr = priceStr + ".00";
    }
    return priceStr;
};

//除法
var _accDiv = function(arg1, arg2) {
    var t1 = 0,
        t2 = 0,
        r1, r2;
    try { t1 = arg1.toString().split(".")[1].length } catch (e) {}
    try { t2 = arg2.toString().split(".")[1].length } catch (e) {}
    with(Math) {
        r1 = Number(arg1.toString().replace(".", ""))
        r2 = Number(arg2.toString().replace(".", ""))
        return (r1 / r2) * pow(10, t2 - t1);
    }
};

//乘法
var _accMul = function(arg1, arg2) {
    var m = 0,
        s1 = arg1.toString(),
        s2 = arg2.toString();
    try { m += s1.split(".")[1].length } catch (e) {}
    try { m += s2.split(".")[1].length } catch (e) {}
    return Number(s1.replace(".", "")) * Number(s2.replace(".", "")) / Math.pow(10, m)
};

//加法
var _accAdd = function(arg1, arg2) {
    var r1, r2, m;
    try { r1 = arg1.toString().split(".")[1].length } catch (e) { r1 = 0 }
    try { r2 = arg2.toString().split(".")[1].length } catch (e) { r2 = 0 }
    m = Math.pow(10, Math.max(r1, r2))
    return (arg1 * m + arg2 * m) / m
}

//得到参数
var _gethash = function() {
    // val样板:window.location.href.slice(window.location.href.indexOf('?'))
    // 返回：
    // {
    //   a:"jhgdsjkfasdfa"
    //   data:"%5B%7B%22goodsNum%22%3A%22191942513806708736%22%2C%22count%22%3A1%2C%22specNum%22%3A%22191942514490380289%22%7D%5D"
    // }
    var val = window.location.href.slice(window.location.href.indexOf('?'));
    var arr = val.replace(/^\?/, "").split("&");
    var params = {};
    for (var i = 0; i < arr.length; i++) {
        var data = arr[i].split("=");
        if (data.length == 2) {
            params[data[0]] = decodeURIComponent(data[1]);
        }
    }
    return params;
};

var AJAX = function(json) {
    // alert(3)
    console.log('参数');
    console.log(json)
    if (json.token) {
        // $.cookie('token')
        if (!_getItem('token')) {
            json.app.$message.error('请登录再试！');
            setTimeout(function() {
                _openHref({ url: './login.html' });
                // window.location.href = './login.html';
            }, 1500);
            return;
        }
    }
    if (json.data) {
        var par = '';
        for (var key in json.data) {
            par += (key + '=' + json.data[key] + '&');
        }
        par = par.slice(0, par.length - 1);
        par = '?' + par;
        json.url += par;
    }
    var header = {};
    json.token ? header.Authorization = _getItem('token') : '';
    $.ajax({
        url: json.url,
        dataType: "json",
        headers: header,
        type: json.type ? json.type : 'POST',
        success: function(data) {
            console.log('返回+++++++' + json.url);
            console.log(data)
            json.success(data);
        },
        error: function(er) {
            // if (json.error) {
            //     json.error(er);
            // }
            json.app.$message.error('服务器开小差了！');
        },
        complete: function(xhr, data) {

        },
    });
};


var _openHref = function(json) {
    // {url:'./a.html',data:{a:"aaa"}}
    var par = '';
    if (json.data) {
        var data = json.data;
        for (var key in data) {
            par += (key + '=' + encodeURIComponent(data[key]) + '&');
        }
        par = par.slice(0, par.length - 1);
        par = '?' + par;
        json.url += par;

    }
    window.location.href = json.url;
};

//每注单价
var PRICE = 2;

var HTTP = 'http://47.244.30.193';


// post    发送验证码
// {
//     mobile:'手机号'   [String]
// }
var SEND_CODE = HTTP + '/Member/SendRegisterCode';



//post     校验注册验证码  注册提交时回传Key
// {
//     mobile:'手机号'      [String]
//     code:'用户输入的验证码'    [String]
// }
var CHECK_CODE = HTTP + '/Member/CheckRegisterCode';


//post     注册  
// {
//     password:'用户的密码'    [String]
//     key:'校验注册验证码回传的key'  [String]
// }
var REGISTER = HTTP + '/Member/Register';



//post     登录   返回的data就是token
// {
//     mobile:'手机号'    [String],
//     password:'密码'    [String]
// }
var LOGIN = HTTP + '/Member/Login';



// post    获取用户信息(要传token)
var GET_USER = HTTP + '/Member/GetMemberInfo';


//post     获取用户余额(要传token)
var GET_BANLANCE = HTTP + '/Member/GetBalance';



//post     修改登录密码
var UPDATA_PAS = HTTP + '/Member/UpdatePassword';
// {
//     password:"旧密码"    [String]
//     newPassword:"新密码"    [String]
// }


//post     修改交易密码
var UPDATE_TRANS_PAS = HTTP + '/Member/UpdatePayPassword';
// {
//     password:"旧密码"    [String]
//     newPassword:"新密码"    [String]
// }


//post   刷新token   每次进入首页刷新一遍  header里面传旧的token
var REFRESH_TOKEN = HTTP + '/Member/RefreshToken';



//post   获取当前可投注的彩旗
// {
//     lotteryType:1, (彩票类型  默认1) [Integer]
//     lotteryId:1    (彩票编号  默认1) [Integer]
// }
var BETROUND = HTTP + '/Lottery/GetNowBetRound';




//获取彩期结果
// {
//     lotteryType:1,  (彩票类型  默认1) [Integer]
//     lotteryId:1,    (彩票编号  默认1) [Integer]
//     roundId:1       (彩期号)  [String]
// }
var ROUND_RESULT = HTTP + '/Lottery/GetRoundResult';



//获取历史彩期
// {
//     lotteryType:1,  (彩票类型  默认1) [Integer]
//     lotteryId:1,    (彩票编号  默认1) [Integer]
//     count:'要获取几条'      [Integer]
// }
var HIS_RESULT = HTTP + '/Lottery/GetRoundResults';



//获取投注列表
// {
//     pageIndex: '页码' [Integer]
//     pageSize: '页码大小' [Integer]
//     desc: '倒叙' [TRUEN]
// }
var GET_BET_LIST = HTTP + '/Bet/GetBet';


//投注
// {

// }
var GO_BET = HTTP + '/Bet/Bet';


//获取银行卡信息
var GETBankS = HTTP + '/Member/GetBankInfo';


//下注  中奖纪录查看
// {
//     logType:'日志类型'    [Integer]  1:投注   2：中奖
//     lastLogId:'最后一个日志编号号, 第一次查询传null'   [long]
//     pageSize:'页码大小'   [Integer]
//     desc:'倒叙'           [true]
// }
var LOG_LIKE = HTTP + '/Member/GetBalanceLogById';


//提交真实姓名 和身份证号
var AUTHER_VERIFIED = HTTP + '/Member/IdentityVerified';


//设置银行卡信息
var BIND_BANK = HTTP + '/Member/SetBankInfo';